using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Controls;
using MyPhotoIndex.ImageBank;
using MyPhotoIndex.DataAccess;

namespace MyPhotoIndex.Settings
{
    public partial class SelectPhotoIndexForm : FormTemplate
    {
        private string m_fileSelected;
        private bool m_createNew;


        public string FileSelected
        {
            get { return m_fileSelected; }
            set { m_fileSelected = value; }
        }

        public bool CreateNew
        {
            get { return m_createNew; }
            set { m_createNew = value; }
        }

        public SelectPhotoIndexForm()
        {
            InitializeComponent();

            foreach (PhotoIndexInfo pii in ApplicationSettings.Default.RecentlyUsedIndexes)
            {
                ListViewItem lvi = new ListViewItem(pii.Name);
                lvi.Tag = pii;
                lvi.SubItems.Add(pii.Path);
                lvi.SubItems.Add(pii.ImageCount.ToString());
                recentlyUsedListView.Items.Add(lvi);

                if (pii.Path.CompareTo(ApplicationSettings.Default.IndexDb) == 0)
                {
                    lvi.Selected = true;
                }
            }

            recentlyUsedListView.MouseDoubleClick += new MouseEventHandler(recentlyUsedListView_MouseDoubleClick);
        }

        void recentlyUsedListView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewItem lvi = recentlyUsedListView.GetItemAt(e.X, e.Y);
            if (lvi != null)
            {
                PhotoIndexInfo pii = lvi.Tag as PhotoIndexInfo;
                if (pii.Path.CompareTo(ApplicationSettings.Default.IndexDb) != 0)
                {
                    this.OkButton_Click(this, EventArgs.Empty);
                }
            }
        }

        protected override void OkButton_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = ApplicationSettings.Default.IndexDb;

            if (recentlyUsedIndexRadioButton.Checked == true)
            {          
                if (recentlyUsedListView.SelectedItems.Count > 0)
                {
                    PhotoIndexInfo pii = recentlyUsedListView.SelectedItems[0].Tag as PhotoIndexInfo;
                    if (pii.Path.CompareTo(ApplicationSettings.Default.IndexDb) != 0)
                    {
                        m_fileSelected = pii.Path;
                    }
                }
            }
            else
                if (existingRadioButton.Checked == true)
                {
                    openFileDialog1.Filter = "MyPhotoIndex Photo index files (*.mdb) | *.mdb";
                    DialogResult dr = openFileDialog1.ShowDialog(this);
                    if (dr != DialogResult.OK)
                    {
                        m_fileSelected = string.Empty;
                    }
                    else
                    {
                        m_fileSelected = openFileDialog1.FileName;
                    }
                }
                else
                {
                    saveFileDialog1.Filter = "MyPhotoIndex Photo index files (*.mdb) | *.mdb";
                    DialogResult dr = saveFileDialog1.ShowDialog(this);
                    if (dr != DialogResult.OK)
                    {
                        m_fileSelected = string.Empty;
                    }
                    else
                    {
                        m_fileSelected = saveFileDialog1.FileName;
                    }
                }

            m_createNew = newRadioButton.Checked;

            base.OkButton_Click(sender, e);
        }
    }
}